/**
   EMMA = Estación Meteorológica de Muestreo Automática
 
 emma_project
 Copyright (C) 2015  Rodrigo Garcia

 This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/* 
#### Sensors flags ####

This file is meant to manage sensors routines using flags.
In these flags each bit is considered a flag, 
1 means activated , 0 the oposite.

A sensor has a flag that represents if the sensor has been activated
to be used, this is the `control flag` and if it is 1 means the
device was marked to read its response and log into memory (SD_card)

The `control flag` is set or cleared by the user through a computer
via serial interface (USART).

To keep `control flag` the same after a poweroff it is used the
EEPROM memory of the micro.

#### Common strings ####


This header is also used to define some strings to be stored on
the FLASH memory, these strings are; Sensor's names and physical
phenomenon names. Those strings are meant to be held completely in
FLASH so they're constants so to use them, there are pointers.

#####  What a sensor should implement #####

 There are certain functions that  sensor should implement to
 make it easier to log sensor's response into a file using
 `Datalogger.c`

###### Initialization ######

  char [sensor_name]_init() 

  Initializes the sensor, returns 0 if initialization OK
  non-zero means error.
  This function setup hardware and ports on the micro, then calls
  *enable* function to read the sensor response and log it. 
  
  If initialization fails no action it returns non-zero (error)
  
###### Enable ######

  void [sensor_name]_enable()
  
  Sets the flag to read the sensor's response, so it will be
  logged in .l and if available .r files
  
###### Disable ######

  void [sensor_name]_disable()
  
  Clear the flag to stop reading the sensor's response, when
  cleared the micro *does not take any action* related to this
  sensor so no data correspondig to this sensor will be logged.

###### Get response ######
 
  float [sensor_name]_get_[physical_phenomenom]()

  Reads the sensor answer, this function calculates the response
  for human readeable format to log this response in the .l file.
  The variable 'response' of the sensor is modified.
  
  If the *sensor reading fails* it is returned SENSOR_NO_ANSWER
  
###### Get Raw Response = (optional) ######
  
  float [sensor_name]_getRAW_[physical_phenomenon]()
  
  Returns RAW response of the sensor if available, this data will
  be logged to the .r file.
  The var `raw_response` is modified

  If the *sensor reading fails* it is returned SENSOR_NO_ANSWER

###### Get Sensor physical phenomenon name (optional) ######

  unsigned char <sensor_name>_PhyisicalPhenIndex()

  Returns the index to the sensor physical phenomenon string in the file `sensors_flags`

*/
#ifndef SENSORS_FLAGS_H

#define SENSORS_FLAGS_H

//#include <avr/pgmspace.h>
#include "EEPROMd.h"

// general value to show a sensor is not responding
#define SENSOR_NO_ANSWER -65534.0

// sensors control variables
unsigned char temperature_ctrl, relative_humidity_ctrl;
// sensors status variables
unsigned char temperature_stat, relative_humidity_stat;

#define TIME 0
#define TEMPERATURE 1
#define RELATIVE_HUMIDITY 2
#define SOIL_MOISTURE 3
#define MAGNETIC_FILED 4
#define GAS 5 // FIXME: which one?
#define PLUVIOMETER 6
#define WINDSPEED 7
#define LIGHTINTENSITY 8
/// ---- EEPROM bits for each sensor ----

// temperature register
#define LM35 0
#define DHT11 1
#define DS18S20 2

// relative humidity register
#define HS1101 0 
// dht11 1
#define SHT1115 2


// --- EEPROM bytes ---
#define TEMPERATURE_CTRL_BYTE1 0x22
#define RELATIVE_HUMIDITY_CTRL_BYTE1 0x30

// ------

// --- Functions ---
/*
  FUNCTION -> Copies the 'control flag' of each sensor from
  EEPROM to its respective RAM variable.
*/
void UpdateFlagsFromEEPROM(void);

/*
 FUNCTION -> sets an EEPROM byte bit
 SetEEPROMFlag(TEMPERATURE_CTRL_BYTE1, LM35);
 sets the TEMPERATURE_CTRL_BYTE1 (0x22) byte, LM35 bit (0)
*/
void SetEEPROMFlag(unsigned char eeprom_addr, unsigned char bit);

/*
 FUNCTION -> clears an EEPROM byte bit
 ClearEEPROMFlag(TEMPERATURE_CTRL_BYTE1, LM35)
 clears the TEMPERATURE_CTRL_BYTE1 (0x22) byte, LM35 bit (0)
*/
void ClearEEPROMFlag(unsigned char eeprom_addr, unsigned char bit);

// to set and clear the status flags
void SetRegister(unsigned char* reg, unsigned char flag);
void ClearRegister(unsigned char* reg, unsigned char flag);

// ------

#endif
